﻿using Autofac;
using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Data.Entities.Settings;

namespace PMS.Services.Authentication
{
    public class AuthenticationProvider : IAuthenticationProvider
    {
        #region Fields
        private readonly CustomerSettings _customerSettings;
        private readonly IComponentContext _componentContext;//Autofac上下文
        #endregion

        #region Ctor
        public AuthenticationProvider(CustomerSettings customerSettings,
            IComponentContext componentContext)
        {
            _customerSettings = customerSettings;
            _componentContext = componentContext;
        }
        #endregion

        #region Utils
        private IAuthenticationService GetAuthenticationService()
        {
            return _customerSettings.AccountAuthenticationType switch
            {
                AuthenticationType.CookieAuth => _componentContext.ResolveNamed<IAuthenticationService>(typeof(CookieAuthenticationService).Name),
                AuthenticationType.JwtAuth => _componentContext.ResolveNamed<IAuthenticationService>(typeof(JwtAuthenticationService).Name),
                _ => null
            };
        }
        #endregion

        #region Properties
        public IAuthenticationService AuthServcice { get => GetAuthenticationService(); }
        #endregion
    }
}
